<?php


namespace App\Libraries\Warehouse;


use App\Libraries\Warehouse\Contracts\DockingBehavior;
use Exception;
use Illuminate\Support\Facades\Log;
use SoapClient;
use SoapFault;

class Docking
{
    /**
     * @var DockingBehavior
     * @author luffyzhao@vip.126.com
     */
    protected $behavior;


    /**
     * setDocking constructor.
     * @param DockingBehavior $behavior
     * @author luffyzhao@vip.126.com
     */
    public function __construct(DockingBehavior $behavior)
    {
        $this->behavior = $behavior;
    }

    /**
     * @throws Exception
     * @author luffyzhao@vip.126.com
     */
    public function request()
    {
        try {
            $client = new SoapClient($this->behavior->getUrl());
            $res = (array)$client->__Call($this->behavior->getCallName(), array('paramters' => $this->behavior->map()));
            if ($res['ask'] === '0') {
                throw new Exception(json_encode($res));
            }

            return $res;
        } catch (SoapFault | Exception $exception) {
            throw new Exception(sprintf('对接[%s] 参数[%s] 失败:%s ', $this->behavior->getCallName(), json_encode($this->behavior->map()), $exception->getMessage()));
        }
    }

}